using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Q.Framework.ValueObject;

namespace Q.Framework.WebControls
{
    [DefaultProperty("Material")]
    [ToolboxData("<{0}:EnderecoControl runat=server></{0}:EnderecoControl>")]
    public class EnderecoControl : CompositeControl, IBindableControl
    {
        public string DataPropertyName { get; set; }

        private String controlBindableProperty = "Value";
        public string ControlBindableProperty
        {
            get { return controlBindableProperty; }
            set { controlBindableProperty = value; }
        }

        public String EstadoPadrao
        {
            get
            {
                ViewState["EstadoPadraoEndereco"] = (ViewState["EstadoPadraoEndereco"] as String) ?? String.Empty;
                return (ViewState["EstadoPadraoEndereco"].ToString());
            }
            set
            {
                ViewState["EstadoPadraoEndereco"] = value;
                if (value != null)
                {
                    txtEstado.Text = value;
                }
                else
                {
                    txtEstado.Text = String.Empty;
                }
            }
        }

        public String CidadePadrao
        {
            get
            {
                ViewState["CidadePadraoEndereco"] = (ViewState["CidadePadraoEndereco"] as String) ?? String.Empty;
                return (ViewState["CidadePadraoEndereco"].ToString());
            }
            set
            {
                ViewState["CidadePadraoEndereco"] = value;
                if (value != null)
                {
                    txtCidade.Text = value;
                }
                else
                {
                    txtCidade.Text = String.Empty;
                }
            }
        }

        private QTextBox txtRua = new QTextBox();
        private QTextBox txtNumero = new QTextBox();
        private QTextBox txtComplemento = new QTextBox();
        private QTextBox txtBairro = new QTextBox();
        private QTextBox txtReferencia = new QTextBox();
        private QTextBox txtCidade = new QTextBox();
        private QTextBox txtEstado = new QTextBox();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);


        }

        private Endereco _value;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public Endereco Value
        {
            get
            {
                if (_value == null)
                {
                    EnsureChildControls();
                    _value = new Endereco();
                    _value.Bairro = txtBairro.Text;
                    _value.Cidade = txtCidade.Text;
                    _value.Complemento = txtComplemento.Text;
                    _value.SiglaUf = txtEstado.Text;
                    _value.Logradouro = txtRua.Text;
                    _value.Numero = txtNumero.Text;
                    _value.Referencia = txtReferencia.Text;
                }
                return _value;
            }

            set
            {
                _value = value; 
                if (value != null)
                {
                    EnsureChildControls();
                    txtBairro.Text = value.Bairro;
                    txtCidade.Text = value.Cidade;
                    txtComplemento.Text = value.Complemento;
                    txtEstado.Text = value.SiglaUf;
                    txtRua.Text = value.Logradouro;
                    txtNumero.Text = value.Numero;
                    txtReferencia.Text = value.Referencia;
                }
            }
        }

        public override void Focus()
        {
            EnsureChildControls();
            txtRua.Focus();
        }

        protected override void CreateChildControls()
        {
            txtRua.ID = this.ID + "Rua";
            txtRua.MaxLength = 200;
            txtRua.Width = new Unit(350, UnitType.Pixel);

            txtNumero.ID = this.ID + "Numero";
            txtNumero.MaxLength = 10;
            txtNumero.Width = new Unit(40, UnitType.Pixel);

            txtComplemento.ID = this.ID + "Complemento";
            txtComplemento.MaxLength = 10;
            txtComplemento.Width = new Unit(170, UnitType.Pixel);

            txtBairro.ID = this.ID + "Bairro";
            txtBairro.MaxLength = 200;
            txtBairro.Width = new Unit(290, UnitType.Pixel);

            txtReferencia.ID = this.ID + "Referencia";
            txtReferencia.MaxLength = 200;
            txtReferencia.Width = new Unit(270, UnitType.Pixel);

            txtCidade.ID = this.ID + "Cidade";
            txtCidade.MaxLength = 200;
            txtCidade.Width = new Unit(290, UnitType.Pixel);

            txtEstado.ID = this.ID + "Estado";
            txtEstado.MaxLength = 2;
            txtEstado.Width = new Unit(30, UnitType.Pixel);
            this.Controls.Add(txtRua);
            this.Controls.Add(txtNumero);
            this.Controls.Add(txtComplemento);
            this.Controls.Add(txtBairro);
            this.Controls.Add(txtReferencia);
            this.Controls.Add(txtCidade);
            this.Controls.Add(txtEstado);
            base.CreateChildControls();
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            writer.AddStyleAttribute("float", "left");
            writer.AddStyleAttribute(HtmlTextWriterStyle.MarginTop, "10px"); 
            writer.RenderBeginTag(HtmlTextWriterTag.Div);                     

            WriteTitleLineDiv(writer);
            WriteTitleDiv(writer, "Logradouro", "400px");
            WriteTitleDiv(writer, "Nº", "85px");
            WriteTitleDiv(writer, "Complemento");
            writer.RenderEndTag();

            WriteFieldLineDiv(writer);
            WriteFieldControl(writer, txtRua, "400px");
            WriteFieldControl(writer, txtNumero, "85px");
            WriteFieldControl(writer, txtComplemento);
            writer.RenderEndTag();



            WriteTitleLineDiv(writer);
            WriteTitleDiv(writer, "Bairro", "340px");
            WriteTitleDiv(writer, "Referência", "180px");
            writer.RenderEndTag();

            WriteFieldLineDiv(writer);
            WriteFieldControl(writer, txtBairro, "340px");
            WriteFieldControl(writer, txtReferencia);
            writer.RenderEndTag();

            WriteTitleLineDiv(writer);
            WriteTitleDiv(writer, "Cidade", "340px");
            WriteTitleDiv(writer, "Estado");
            writer.RenderEndTag();

            WriteFieldLineDiv(writer);
            WriteFieldControl(writer, txtCidade, "340px");
            WriteFieldControl(writer, txtEstado);
            writer.RenderEndTag();


            writer.RenderEndTag();                                              
        }

        private void WriteFieldControl(HtmlTextWriter writer, QTextBox textBox, String Width = "")
        {
            if (!String.IsNullOrWhiteSpace(Width))
            {
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, Width);
                writer.AddStyleAttribute("float", "left");
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            textBox.RenderControl(writer);
            writer.RenderEndTag();
        }

        private static void WriteTitleLineDiv(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "25px");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        private static void WriteFieldLineDiv(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "30px");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        private static void WriteTitleDiv(HtmlTextWriter writer, String Title, String Width = "")
        {
            if (!String.IsNullOrWhiteSpace(Width))
            {
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, Width);
                writer.AddStyleAttribute("float", "left");
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.Write(Title);
            writer.RenderEndTag();
        }

        public void SetRequiredFields(Boolean rua = false, Boolean numero = false, Boolean complemento = false, Boolean bairro = false, Boolean referencia = false, Boolean cidade = false, Boolean estado = false)
        {
            EnsureChildControls();
            txtBairro.Required = bairro;
            txtCidade.Required = cidade;
            txtComplemento.Required = complemento;
            txtEstado.Required = estado;
            txtRua.Required = rua;
            txtNumero.Required = numero;
            txtReferencia.Required = referencia;
        }
    }
}